<html>
<head><meta charset="utf-8"><title>SourceScopeData&#x27;s local_data · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html">SourceScopeData&#x27;s local_data</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227553312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227553312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227553312">(Feb 24 2021 at 06:16)</a>:</h4>
<p>#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]<br>
pub struct SourceScopeData {<br>
    pub span: Span,<br>
    pub parent_scope: Option&lt;SourceScope&gt;,</p>
<div class="codehilite"><pre><span></span><code>/// Crate-local information for this source scope, that can&#39;t (and
/// needn&#39;t) be tracked across crates.
pub local_data: ClearCrossCrate&lt;SourceScopeLocalData&gt;,
</code></pre></div>

<p>}</p>
<p>Can I ask why SourceScopeData only cares about local_data? Does it mean we can not tell if a MIR statement (from other crate) is unsafe or not?</p>



<a name="227568845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227568845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227568845">(Feb 24 2021 at 09:13)</a>:</h4>
<blockquote>
<p>Does it mean we can not tell if a MIR statement (from other crate) is unsafe or not?</p>
</blockquote>
<p>Correct. <code>local_data</code> needs to be cleared cross-crate as it contains <code>HirId</code>'s, which are only valid within the current crate as HIR is not serialized into the metadata. Not clearing it would cause an ICE.</p>



<a name="227569154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227569154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227569154">(Feb 24 2021 at 09:16)</a>:</h4>
<p><span class="user-mention" data-user-id="382085">@Shihao Xia</span></p>



<a name="227698216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227698216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227698216">(Feb 25 2021 at 00:14)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> Thank you for the confirmation.</p>



<a name="227698436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227698436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227698436">(Feb 25 2021 at 00:17)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> Could you enlighten me, in the MIR to LLVM IR step, if I want to obtain a MIR statement's source scope data(local data's safety) from external crate, how can I do that ?</p>



<a name="227726449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227726449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227726449">(Feb 25 2021 at 07:03)</a>:</h4>
<p>You can't. The crate metadata simply doesn't contain any knowledge about source scope safety.</p>



<a name="227726456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227726456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227726456">(Feb 25 2021 at 07:03)</a>:</h4>
<p>Why do you need it?</p>



<a name="227777587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227777587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227777587">(Feb 25 2021 at 14:44)</a>:</h4>
<p>I want to do some analysis during MIR, however knowing MIR statement is from safe block or not is one of requirements..</p>



<a name="227778157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227778157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227778157">(Feb 25 2021 at 14:48)</a>:</h4>
<p>If you're working on a tool that operates outside of rustc itself, I imagine that your best shot (or at least most straightforward approach) would be to try to correlate between the span locations associated with the MIR statement and AST/HIR produced out-of-band…?</p>



<a name="227781439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227781439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227781439">(Feb 25 2021 at 15:08)</a>:</h4>
<p>that is workable. My thought is I can output source scope data per rlib same like .rmeta</p>



<a name="227781604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227781604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227781604">(Feb 25 2021 at 15:09)</a>:</h4>
<p>When codegen a specific MIR statement, looking for this MIR statement belongs to which crate? And then find if MIR statement from a safe block or not?</p>



<a name="227786570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227786570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227786570">(Feb 25 2021 at 15:38)</a>:</h4>
<p>Well, codegen doesn't particularly care about safety of statements, but conceptually if it needed to, I would approach this in a manner like this. I would grab HIR and check against that… That said, you may be interested in <code>rustc_mir/src/transform/check_unsafety.rs</code></p>



<a name="227793637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227793637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227793637">(Feb 25 2021 at 16:20)</a>:</h4>
<p>Yes that would work...but still, the problem is when compiling the a crate(local crate), extern crate's safety information is still inaccessible since .rmeta doesn't have that, please correct me if I am wrong</p>



<a name="227793836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227793836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227793836">(Feb 25 2021 at 16:21)</a>:</h4>
<p>Yeah, that's correct, you would have to collect the information yourself while the crate graph is built/analyzed.</p>



<a name="227793887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227793887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227793887">(Feb 25 2021 at 16:21)</a>:</h4>
<p>Could you enlighten me how rustc load MIR or HIR from .rmeta?</p>



<a name="227794064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227794064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227794064">(Feb 25 2021 at 16:22)</a>:</h4>
<p>[INFO  rustc_metadata::locator] lib candidate: /home/xsh/code/rust-compiler/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-df6bb7d1baae24d3.rlib<br>
[INFO  rustc_metadata::locator] lib candidate: /home/xsh/code/rust-compiler/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-df6bb7d1baae24d3.so<br>
[INFO  rustc_metadata::locator] rlib reading metadata from: /home/xsh/code/rust-compiler/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-df6bb7d1baae24d3.rlib<br>
[INFO  rustc_metadata::locator] reading "libstd-df6bb7d1baae24d3.rlib" =&gt; 54.8µs<br>
[INFO  rustc_metadata::creader] register crate <code>std</code> (private_dep = false)</p>



<a name="227794101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227794101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227794101">(Feb 25 2021 at 16:22)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/tree/master/compiler/rustc_metadata">https://github.com/rust-lang/rust/tree/master/compiler/rustc_metadata</a> is the relevant crate.</p>



<a name="227794424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227794424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227794424">(Feb 25 2021 at 16:23)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_metadata/src/rmeta/decoder.rs#L1147">https://github.com/rust-lang/rust/blob/master/compiler/rustc_metadata/src/rmeta/decoder.rs#L1147</a> seems like a relevant entrypoint, but I'm guessing here.</p>



<a name="227794500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227794500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227794500">(Feb 25 2021 at 16:23)</a>:</h4>
<p>Thanks I will take a look</p>



<a name="227794997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/SourceScopeData%27s%20local_data/near/227794997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shihao Xia <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/SourceScopeData&#x27;s.20local_data.html#227794997">(Feb 25 2021 at 16:26)</a>:</h4>
<p>Seems like this is it! <span class="user-mention" data-user-id="123586">@nagisa</span> Thanks a lot, huge help</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>